<?php
ini_set('memory_limit', '-1');
require_once("Zend/Loader.php");
require_once('Models/Record.php');

Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Analytics');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

ini_set('max_execution_time', 300); 

$client = Zend_Gdata_ClientLogin::getHttpClient(
    "reader.happiness@gmail.com",
    "demolarh",
    Zend_Gdata_Analytics::AUTH_SERVICE_NAME
);

$service = new Zend_Gdata_Analytics($client);
 
$dimensions = array(
	Zend_Gdata_Analytics_DataQuery::DIMENSION_LANDING_PAGE_PATH,  
	//Zend_Gdata_Analytics_DataQuery::DIMENSION_PAGE_PATH,
	Zend_Gdata_Analytics_DataQuery::DIMENSION_PAGE_TITLE,
	Zend_Gdata_Analytics_DataQuery::DIMENSION_DAY,
	Zend_Gdata_Analytics_DataQuery::DIMENSION_MONTH,
	Zend_Gdata_Analytics_DataQuery::DIMENSION_YEAR,
	Zend_Gdata_Analytics_DataQuery::DIMENSION_HOUR,
);

$metrics = array(
	Zend_Gdata_Analytics_DataQuery::METRIC_VISITS,
	Zend_Gdata_Analytics_DataQuery::METRIC_PAGEVIEWS_PER_VISIT,
	Zend_Gdata_Analytics_DataQuery::METRIC_TIME_ON_PAGE,
	Zend_Gdata_Analytics_DataQuery::METRIC_AVG_TIME_ON_PAGE,
	Zend_Gdata_Analytics_DataQuery::METRIC_BOUNCES,
	Zend_Gdata_Analytics_DataQuery::METRIC_VISITORS,
	Zend_Gdata_Analytics_DataQuery::METRIC_PAGEVIEWS

);

$today = strtotime("today 00:00");
$yesterday =  strtotime("-1 day", $today);

$query = $service->newDataQuery()
    ->setProfileId(31405012)
    ->setStartDate(date('Y-m-d', $yesterday))
    ->setEndDate(date('Y-m-d', $yesterday + (60 *60 *24)))
    ->setMaxResults(10000); 

foreach($dimensions as $dim){  
		$query->addDimension($dim);  
}  

	foreach($metrics as $met){  
		$query->addMetric($met);  
}  

$result = $service->getDataFeed($query);


if(INSERT_DATA)
{
	foreach($result as $row){
		$record = new Record;
		$record->landing_page 			= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_LANDING_PAGE_PATH);
		$record->page_title 			= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_PAGE_TITLE);
		$record->page_views 			= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_PAGEVIEWS);
		$record->visits 			= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS);
		$record->visitors 			= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_VISITORS);
		$record->bounce_rate 			= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_BOUNCES);
		$record->pageviews_per_visit            = (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_PAGEVIEWS_PER_VISIT);
		$record->time_on_page 			= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_TIME_ON_PAGE);
		$record->avg_time_on_page 		= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_AVG_TIME_ON_PAGE);
		$record->day 				= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_DAY);
		$record->month 				= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_MONTH);
		$record->year 				= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_YEAR);
		$record->hour 				= (string)$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_HOUR);
		$record->save();
	}
}

if(DATA_INDEX_SHOW)
{
	echo "<table border='1' rowspan='0' colspan='0'>\n";
	echo "\t<tr>\n";
	echo "\t\t<td>#</td>";
	echo "\t\t<td>Landing page</td>";
	echo "\t\t<td>Page title</td>";
	echo "\t\t<td>Visits</td>";
	echo "\t\t<td>Page views</td>";
	echo "\t\t<td>Visitors</td>";
	echo "\t\t<td>Pagevies per visit</td>";
	echo "\t\t<td>Time on page</td>";
	echo "\t\t<td>AVG Time on page</td>";
	echo "\t\t<td>Bounces rate</td>";
	echo "\t\t<td>Day</td>";
	echo "\t\t<td>Month</td>";
	echo "\t\t<td>Year</td>";
	echo "\t\t<td>Hour</td>";
	echo "\t</tr>\n";

    $i = 1;
	foreach($result as $row){
		echo "\t<tr>\n";
		echo "\t\t<td>". $i++ ."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_LANDING_PAGE_PATH)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_PAGE_TITLE)."</td>\n";  
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_PAGEVIEWS)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_VISITORS)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_PAGEVIEWS_PER_VISIT)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_TIME_ON_PAGE)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_AVG_TIME_ON_PAGE)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::METRIC_BOUNCES)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_DAY)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_MONTH)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_YEAR)."</td>\n";
		echo "\t\t<td>".$row->getValue(Zend_Gdata_Analytics_DataQuery::DIMENSION_HOUR)."</td>\n";
		echo "\t<tr>\n";  
	}
	echo "</table>\n";
}